﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using DbUtility;

namespace Web.Areas.Admin.Controllers
{
    public class ScreenController : BaseController
    {
        public ActionResult ScoreWall()
        {
            playerLogic pl = new playerLogic();
            var list = pl.Getplayers(new player(), "", "sort");
            foreach (var item in list)
            {
                item.score = item.average.ToString();
            }
            if (Request.IsAjaxRequest())
            {
                return Json(list, JsonRequestBehavior.AllowGet);
            }
            return View(list);
        }
        public ActionResult Lottery()
        {
            lotterylogLogic ll = new lotterylogLogic();
            wx_usersLogic ul = new wx_usersLogic();

            var list = ll.Getlotterylogs(new lotterylog() { isclear = false }); //已经中奖并且未被清除的数据
            if (Request.IsAjaxRequest())
            {
                return PartialView("_Lottery", list);
            }

            var allUser = ul.Getwx_userss(new wx_users());
            ViewBag.Imgs = allUser.Select(o => o.headimgurl).ToList();
            return View(list);
        }
        /// <summary>
        /// 只有参与过打分或者调研的用户才有资格抽奖
        /// </summary>
        [HttpPost]
        public ActionResult GetLottery(int count)
        {
            try
            {
                wx_usersLogic ul = new wx_usersLogic();
                lotterylogLogic ll = new lotterylogLogic();
                var sql = string.Format("select top {0} * from wx_users where isdeleted=0 and (id in (select distinct userid from scorelog where isdeleted=0) or id in (select distinct userid from useranswer where isdeleted=0)) and id not in (select userid from lotterylog where isdeleted = 0) order by NEWID()", count);
                var lotteryUsers = ul.Getwx_userss(sql, 1, int.MaxValue).PageList.ToList();
                if (lotteryUsers.Count <= 0)
                {
                    return ErrorMsg("暂无可中奖的用户！");
                }
                else
                {
                    foreach (var user in lotteryUsers)
                    {
                        lotterylog newLottery = new lotterylog()
                        {
                            createdate = DateTime.Now,
                            createuserid = 0,
                            isdeleted = false,
                            userid = user.id,
                            nickname = user.nickname,
                            isclear = false
                        };
                        var result = ll.Add(newLottery);
                    }
                    return SuccessMsg(string.Format("恭喜{0}中奖！", string.Join(",", lotteryUsers.Select(o => o.nickname))));
                }
            }
            catch (Exception e)
            {
                return ErrorMsg("您的网络不稳定，请稍后再试！");
            }
        }

        /// <summary>
        /// 清除抽奖数据(isClear=true:从大屏幕列表中清除；isClear=false:标记数据库数据为isdeleted=true)
        /// </summary>
        [HttpPost]
        public ActionResult ClearLottery(bool isClear)
        {
            try
            {
                DBContext db = new DBContext();
                var sql = string.Empty;
                if (isClear)
                {
                    sql = "update lotterylog set isclear = 1 where isclear = 0";
                }
                else
                {
                    sql = "update lotterylog set isdeleted=1 where isdeleted = 0";
                }
                var r = db.ExecuteNonQuerySql(sql);
                return SuccessMsg("已清除！");
            }
            catch (Exception e)
            {
                return ErrorMsg("您的网络不稳定，请稍后再试！");
            }
        }
    }
}
